//
//  NetworkHandle.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/29.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "NetworkHandle.h"
#import "SJHomeModel.h"
#import "CommentModel.h"
#import "NextGoodModel.h"
#import "SDCommentModel.h"
#import "AddressModel.h"
#import "UserModel.h"
#import "GoodNameModel.h"
#import "PropertyModel.h"

@implementation NetworkHandle

+ (void)removeFromWindow
{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [MBProgressHUD hideAllHUDsForView:app.window animated:YES];

}

+ (void)addToWindow
{
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [MBProgressHUD showMessage:@"网络加载中" toView:app.window];

}

+ (void)getDataWithUrl:(NSString *)urlStr firstKey:(NSString *)firstKey secondKey:(NSString *)secondKey  success:(void(^)(NSDictionary *dictionary))success
{
    // [NetworkHandle addToWindow];
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // [NetworkHandle removeFromWindow];
        NSDictionary *dictionary = responseObject[firstKey];
        NSDictionary *dic = dictionary[secondKey];
        success(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
}


+ (void)getUpDataWithUrl:(NSString *)urlStr modelClass:(Class)classname firstKey:(NSString *)firstKey secondKey:(NSString *)secondKey  success:(void(^)(NSArray *array))success
{
    //[NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       // [NetworkHandle removeFromWindow];
        NSMutableArray *tempArray = [NSMutableArray array];
        NSDictionary *dictionary = responseObject[firstKey];
        NSArray *array = dictionary[secondKey];
        for (NSDictionary *dic in array) {
            SJHomeModel *model = [[classname alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [tempArray addObject:model];
        }
        success(tempArray);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
}


+ (void)getDataWithUrl:(NSString *)urlStr dictionary:(NSDictionary *)dict success:(void(^)(NSDictionary *dictionary))success
{
    // [NetworkHandle addToWindow];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    [signDict setValue:[Tools signDict:dict] forKey:@"sign"];
    
    [manager POST:urlStr parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // [NetworkHandle removeFromWindow];
        NSMutableDictionary *dataDic =[NSMutableDictionary dictionary];
        // NSLog(@"r --- %@", responseObject);
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            NSDictionary *dictionary = responseObject[@"data"];
            NSDictionary *dic = dictionary[@"current_period"];
           // [dataDic setValue:dic forKey:@"current"];
            [dataDic setObject:dic forKey:@"current"];
            NSMutableArray *tempArr = [NSMutableArray array];
            
            NSDictionary *dict0 = dictionary[@"comment_list"];
            NSArray *array = dict0[@"list"];
            for (NSDictionary *dic in array) {
                CommentModel *comModel =[[CommentModel alloc] init];
                [comModel setValuesForKeysWithDictionary:dic];
                comModel.curr_page = dict0[@"curr_page"];
                comModel.total = dict0[@"total"];
                comModel.total_page = dict0[@"total_page"];
                [tempArr addObject:comModel];
            }
            [dataDic setValue:tempArr forKey:@"comment"];
            
            if (dictionary[@"next_period_list"] != nil) {
                NSMutableArray *tempArray = [NSMutableArray array];
                NSArray *arr = dictionary[@"next_period_list"];
                for (NSDictionary *dic in arr) {
                    NextGoodModel *model = [[NextGoodModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [tempArray addObject:model];
                }
                [dataDic setValue:tempArray forKey:@"next"];
            }
            
            success(dataDic);
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Tools showAlert:dataDic[@"message"]];
            });
        }
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
    
}

+ (void)getInfoWithUrl:(NSString *)url dictionary:(NSDictionary *)dic success:(void(^)(NSDictionary *dictionary))success
{
    //[NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [signDict setValue:[Tools signDict:dic] forKey:@"sign"];
    [manager POST:url parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       // [NetworkHandle removeFromWindow];
        NSDictionary *dict = responseObject;
        success(dict);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}
//商品列表请求
+ (void)getUpDataWithUrl:(NSString *)urlStr modelClass:(Class)classname firstKey:(NSString *)firstKey success:(void (^)(NSArray *array))success
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
   // [NetworkHandle addToWindow];
    [manager POST:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // [NetworkHandle removeFromWindow];
        NSMutableArray *tempArray = [NSMutableArray array];
        NSArray *array= responseObject[firstKey];
        for (NSDictionary *dic in array) {
            GoodNameModel *model = [[classname alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [tempArray addObject:model];
        }
        success(tempArray);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];

}

+ (void)getUpDataWithUrl:(NSString *)urlStr dictionary:(NSDictionary *)dictionary firstKey:(NSString *)firstKey success:(void(^)(NSArray *array))success
{
    //[NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    [signDict setValue:[Tools signDict:dictionary] forKey:@"sign"];
    [manager POST:urlStr parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       // [NetworkHandle removeFromWindow];
        NSDictionary *dataDic = responseObject;
            if ([dataDic[@"code"] isEqualToString:@"200"]) {
                NSDictionary *dictionary = dataDic[firstKey];
                NSArray *array = dictionary[@"list"];
                NSMutableArray *temparray = [NSMutableArray array];
                for (NSDictionary *dict in array) {
                    CommentModel *model = [[CommentModel alloc] init];
                    [model setValuesForKeysWithDictionary:dict];
                    model.total = dictionary[@"total"];
                    model.total_page = dictionary[@"total_page"];
                    model.curr_page = dictionary[@"curr_page"];
                    [temparray addObject:model];
                }
                success(temparray);
            } else {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [Tools showAlert:dataDic[@"message"]];
                });
            }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

+ (void)getUpDataWithUrl:(NSString *)urlStr class:(Class)classname dictionary:(NSDictionary *)dictionary firstKey:(NSString *)firstKey secondKey:(NSString *)secondKey success:(void(^)(NSArray *array))success
{
    // [NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    [signDict setValue:[Tools signDict:dictionary] forKey:@"sign"];
    [manager POST:urlStr parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       // [NetworkHandle removeFromWindow];
        NSDictionary *dataDic = responseObject;
        // NSLog(@"%@", responseObject);
        if ([dataDic[@"code"] isEqualToString:@"200"]) {
            NSDictionary *dict = dataDic[firstKey];
            NSArray *array = dict[secondKey];
            NSMutableArray *temparray = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                CommentModel *model = [[classname alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [temparray addObject:model];
            }
            success(temparray);
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Tools showAlert:dataDic[@"message"]];
            });
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
}
//收货地址请求
+ (void)getUpDataWithUrl:(NSString *)urlStr class:(Class)classname dictionary:(NSDictionary *)dictionary firstKey:(NSString *)firstKey success:(void(^)(NSArray *array))success
{
    // [NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    [signDict setValue:[Tools signDict:dictionary] forKey:@"sign"];
    [manager POST:urlStr parameters:signDict success:^(NSURLSessionDataTask *task, id responseObject) {
        // [NetworkHandle removeFromWindow];
        NSDictionary *dataDic = responseObject;
        if ([dataDic[@"code"] isEqualToString:@"200"]) {
            NSArray *array = dataDic[firstKey];
            NSMutableArray *temparray = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                AddressModel *model = [[classname alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [temparray addObject:model];
            }
            success(temparray);
        } else  {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Tools showAlert:dataDic[@"message"]];
            });
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    

    
}
+ (void)registAndLoginByUrl:(NSString *)url dict:(NSDictionary *)dic success:(void(^)(NSDictionary *dictionary))success
{
   // [NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [signDict setValue:[Tools signDict:dic] forKey:@"sign"];
    [manager POST:url parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       // [NetworkHandle removeFromWindow];
        NSDictionary *dictionary = responseObject;
        NSDictionary *dic1 = dictionary[@"data"];
        UserModel *model = [[UserModel alloc] init];
        [model setValuesForKeysWithDictionary:dic1];
            
        NSDictionary *dict = @{@"code":dictionary[@"code"], @"model":model};
        success(dict);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
            [NetworkHandle removeFromWindow];
        }];
    
    
}
//公共资源
+ (void)getPropertyUrl:(NSString *)url dict:(NSDictionary *)dic success:(void(^)(NSDictionary *dictionary))success
{
    // [NetworkHandle addToWindow];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [signDict setValue:[Tools signDict:dic] forKey:@"sign"];
    [manager POST:url parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // [NetworkHandle removeFromWindow];
        NSDictionary *dictionary = responseObject;
        NSDictionary *dic1 = dictionary[@"data"];
        PropertyModel *PModel = [[PropertyModel alloc] init];
        [PModel setValuesForKeysWithDictionary:dic1];
        
        NSDictionary *dict = @{@"code":dictionary[@"code"], @"PModel":PModel};
        success(dict);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
    
    
}

+ (void)getDataWithUrl:(NSString *)urlStr class:(Class)classname dictionary:(NSDictionary *)dictionary firstKey:(NSString *)firstKey success:(void(^)(NSArray *array))success
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    [signDict setValue:[Tools signDict:dictionary] forKey:@"sign"];
    NSLog(@"%@-----%@", urlStr, signDict);
    [manager POST:urlStr parameters:signDict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // [NetworkHandle removeFromWindow];
        NSDictionary *dataDic = responseObject;
        // NSLog(@"%@", responseObject);
        if ([dataDic[@"code"] isEqualToString:@"200"]) {
           ;
            NSArray *array = dataDic[firstKey];
            NSMutableArray *temparray = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                CommentModel *model = [[classname alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [temparray addObject:model];
            }
            success(temparray);
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Tools showAlert:dataDic[@"message"]];
            });
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];

}



@end
